package com.atguigu.yygh.order.controller.api;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.AuthContextHolder;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.order.service.OrderService;
import com.atguigu.yygh.vo.order.OrderCountQueryVo;
import com.atguigu.yygh.vo.order.OrderQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author ybl
 * @version 1.9
 * @date 2021/5/23 20:04
 */
@RestController
@RequestMapping("api/order/orderInfo")
public class OrderApiController {

  @Autowired
  private OrderService orderService;

  @ApiOperation(value = "获取订单统计数据")
  @PostMapping("inner/getCountMap")
  public Map<String, Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo) {
    return orderService.getCountMap(orderCountQueryVo);
  }

  @ApiOperation(value = "取消预约")
  @GetMapping("auth/cancelOrder/{orderId}")
  public Result cancelOrder(
      @ApiParam(name = "orderId", value = "订单id", required = true)
      @PathVariable("orderId") Long orderId) {
    return Result.ok(orderService.cancelOrder(orderId));
  }


  // 创建订单
  @ApiOperation(value = "创建订单")
  @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
  public Result submitOrder(@PathVariable String scheduleId,@PathVariable Long patientId) {
    return Result.ok(orderService.saveOrder(scheduleId,patientId));
  }

  //订单列表（条件查询带分页下`）
  @GetMapping("auth/{page}/{limit}")
  public Result list(@PathVariable Long page,
                     @PathVariable Long limit,
                     OrderQueryVo orderQueryVo, HttpServletRequest request) {
    orderQueryVo.setUserId(AuthContextHolder.getUserId(request));
    Page<OrderInfo> pageParam = new Page<>(page,limit);
    IPage<OrderInfo> pageModel =
        orderService.selectPage(pageParam,orderQueryVo);
    return Result.ok(pageModel);

  }

  //根据订单id查询订单详情
  @GetMapping("auth/getOrders/{orderId}")
  public Result getOrders(@PathVariable String orderId) {
    OrderInfo orderInfo = orderService.getOrder(orderId);
    return Result.ok(orderInfo);
  }
}
